/**
 * 
 */
package com.kunbo.cn.app.service.impl;
import com.kunbo.cn.util.date.CommonsUtil;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.app.service.SaleCustomerOrderService;

/**
 *
 * @author bjl  
 * @date 2019年3月26日 
 */
@Service
public class SaleCustomerOrderServiceImpl  implements SaleCustomerOrderService {
	@Autowired
	private JdbcTemplate jdbc;
	
	@Override
	public String getTitle(String login_name,String title) {
		String n = "";
		if(title.equals("")) {
			String sql="SELECT * from business_customer_users_manage where 1=1";
			sql +=" and login_name =  '" + login_name + "'";
			System.out.println("（*************login_name列表**************）"+login_name);
			List<Map<String, Object>> one = jdbc.queryForList(sql);
			String t = String.valueOf(one.get(0).get("customer").toString());
			
			System.out.println("*************login_name列表**************"+t);
			String sql1 = "SELECT \r\n" + 
					"	s.* \r\n" + 
					"FROM \r\n" + 
					"	business_sale_order_manage s \r\n" + 
					"WHERE   1 = 1 ";
			if(t != null && !t.isEmpty()){
				sql1 += " and s.customerid = '" + t + "'";
			}
			List<Map<String, Object>> list = jdbc.queryForList(sql1);
			n = String.valueOf(list.get(0).get("customer").toString());
		}else if(title.equals("business_supplier_users_manage")) {
		}
		return n;
	}
	@Override
	public List<Map<String, Object>> matter(String type) {
		String sql  = "";
		if(type.equals("user")) {
			sql = "select id,name from business_materials_manage m  where  m.mater_type in('精煤','副产品') ";
		}else {
			sql = "select id,name from business_materials_manage m  where  m.mater_type in('副产品') ";
		}
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>> getSaleOrder(String login_name) {
		String sql="SELECT * from business_customer_users_manage where 1=1";
		sql +=" and login_name =  '" + login_name + "'";
		System.out.println("（*************login_name列表**************）"+login_name);
		List<Map<String, Object>> one = jdbc.queryForList(sql);
		String t = String.valueOf(one.get(0).get("customer").toString());
		String sql1 = "SELECT \r\n" + 
				"	s.* \r\n" + 
				"FROM \r\n" + 
				"	business_sale_order_manage s \r\n" + 
				"WHERE   1 = 1 ";
		if(t != null && !t.isEmpty()){
			sql1 += " and s.customerid = '" + t + "'";
		}
		List<Map<String, Object>> list = jdbc.queryForList(sql1);
		return list;
	}
	public List<Map<String, Object>> getCustomerOrder(String login_name, String materials, String sale_order_code,
			String start_time, String end_time,Integer page,Integer pageSize) {
		String sql="SELECT * from business_customer_users_manage where 1=1";
		sql +=" and login_name =  '" + login_name + "'";
		System.out.println("（*************login_name列表**************）"+login_name);
		List<Map<String, Object>> one = jdbc.queryForList(sql);
		String t = String.valueOf(one.get(0).get("customer").toString());//customerid
	System.out.println("（*************t列表**************）"+t);
			String sql1 = "SELECT \r\n" + 
					"	s.* \r\n" + 
					"FROM \r\n" + 
					"	business_sale_order_manage s \r\n" + 
					"WHERE   1 = 1 ";
			if(t != null && !t.isEmpty()){
				sql1 += " and s.customerid = '" + t + "'";
			}
			if(materials != null && !materials.isEmpty()){
				sql1 += " and s.materials =  '" + materials + "'";
			}
			if(sale_order_code != null && !sale_order_code.isEmpty()){
				sql1 += " and s.sale_order_code like '%" + sale_order_code + "%'";
			}
			if(start_time != null && !start_time.isEmpty()){
				sql1 += " and s.create_time >= '" + start_time + "'";
			}
			
			if(end_time != null && !end_time.isEmpty()) {
				 Long cTime= CommonsUtil.stringToTime(end_time)+86400000L;//时间加1天
				 String end=CommonsUtil.timeToString(cTime);
				sql1 += " and s.create_time < '" + end + "'";
			}
			System.out.println("（*************sql1列表**************）"+sql1);
			sql1 += " and s.state <> 5 and s.state <> 6 " ;
			sql1 += " order by id  desc  limit " + (page * pageSize) +"," + pageSize;
			System.out.println(sql+"<!!!!!!!!!!!!!!!!!!!!");
			List<Map<String, Object>> list = jdbc.queryForList(sql1);
			return list;
	}
	@Override
	public List<Map<String, Object>> Orderdetails(Integer id) {
		String sql="SELECT * from business_sale_order_manage where id="+id+"";
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> noticeList(String login_name,Integer page,Integer pageSize) {
		String sql="SELECT\r\n" + 
				"	c. NAME as customer\r\n" + 
				"FROM\r\n" + 
				"	business_customer_users_manage w,\r\n" + 
				"	business_customer_manage c\r\n" + 
				"WHERE 1 = 1\r\n" + 
				"AND c.id = w.customer";
		sql +=" and w.login_name =  '" + login_name + "'";
		System.out.println("（*************login_name列表**************）"+login_name);
		List<Map<String, Object>> one = jdbc.queryForList(sql);
		String t = String.valueOf(one.get(0).get("customer").toString());//customerid
		String sql1 = "SELECT \r\n" + 
				"	d.* \r\n" + 
				"FROM \r\n" + 
				"	business_delivery_notice_manage d \r\n" + 
				"WHERE   1 = 1 ";
		if(t != null && !t.isEmpty()){
			sql1 += " and d.receive_unit = '" + t + "'";
		}
		sql1 += " order by id desc limit " + (page * pageSize) +"," + pageSize;
		List<Map<String, Object>> list = jdbc.queryForList(sql1);
		for (Map<String, Object> map : list) {
			//String mater_type_sql = "select name from business_matter_type_manage where name = '" + map.get("mater_type").toString() +"'";
			//String mater_type_sql = "select name from business_matter_type_manage where id = " + Integer.parseInt(map.get("mater_type").toString()) ;
			String material_sql = "select text from select_waybill_order where id = '" +Integer.parseInt( map.get("state").toString()) +"'";
			//String wellhead_sql = "select text from select_wellhead where id = '" +Integer.parseInt( map.get("wellhead").toString()) +"'";//String mater_type_sql = "select name from business_matter_type_manage where id = " +  String.valueOf(map.get("mater_type").toString()) ;
			if(jdbc.queryForList(material_sql) != null && jdbc.queryForList(material_sql).size() > 0) {
				map.put("sate", jdbc.queryForList(material_sql).get(0).get("text"));
			}
		}
		return list;
	}
	@Override
	public List<Map<String, Object>> noticedetails(Integer id) {
		String sql="SELECT * from business_delivery_notice_manage where id="+id+"";
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		for (Map<String, Object> map : list) {
			String material_sql = "select text from select_waybill_order where id = '" +Integer.parseInt( map.get("state").toString()) +"'";
			if(jdbc.queryForList(material_sql) != null && jdbc.queryForList(material_sql).size() > 0) {
				map.put("sate", jdbc.queryForList(material_sql).get(0).get("text"));
			}
		}
		return list;
	}
}
